<link href="/css/style.css" rel="stylesheet">
<link href="/css/style-responsive.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/js/bootstrap-wysihtml5/bootstrap-wysihtml5.css"/>
<!--////	alert(JSON.stringify(response));-->

<body class="panel" style="padding: 20;">
<div class="row">
    <div class="col-sm-12">
        <section class="panel">
            <header class=" custom-tab turquoise-tab">
                <ul class="nav nav-tabs" id="appTabList">

                </ul>
            </header>
            <div class="panel-body">
                <div class="tab-content" id="appTableContentList">

                </div>
                <footer>
                    <label>
                        <select class="form-control" id="stepInput" onChange="currentPatchPage = 1;changeStep(false)"
                                size="1" name="hidden-table-info_length" aria-controls="hidden-table-info">
                            <option value="10" selected="selected">10</option>
                            <option value="25">25</option>
                            <option value="50">50</option>
                            <option value="100">100</option>
                        </select> records per page</label>

                    <div id="pageLabelDiv" class="dataTables_paginate paging_bootstrap pagination">
                        <ul>
                        </ul>
                    </div>
                </footer>
            </div>
        </section>
    </div>
</div>
<!--	//-----------弹出创建app的窗口------start------------>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">新增APP</h4>
            </div>

            <div class="modal-body row">
                <div class="col-md-5">
                    <div class="form-group">
                        <label>App的名称</label>
                        <input type="text" class="form-control" id="createApp_bundle_id" name="createApp_bundle_id"
                               required="required"/>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label>所属平台</label>
                        <select id="createApp_platform" name="createApp_platform" class="form-control">
                            <option value="0">iphone</option>
                            <option value="1">android</option>
                            <option value="2">ipad</option>
                            <option value="3">android pad</option>
                        </select>

                    </div>
                </div>

                <div class="col-md-3">
                    <div class="form-group">
                        <label>启用</label> <br/>
                        <input type="checkbox" id="appEnable" name="appEnable" checked/>
                    </div>
                </div>

            </div>
            <div class="modal-body row">

                <div class="col-md-12">
                    <div class="form-group">
                        <label>描述</label>
                        <textarea class="wysihtml5 form-control" id="createApp_description" name="createApp_description"
                                  rows="9"></textarea>
                    </div>
                </div>

            </div>
            <div class="modal-body row">
                <div class="col-md-5">
                </div>
                <div class="col-md-2">
                    <div class="form-group" align="middle">
                        <button type="button" class="btn btn-success  btn-sm" onClick="addAppItem()">Save</button>
                    </div>
                </div>

                <div class="col-md-5">

                    <img src="images/input-spinner.gif" id="busyImg_saveAPP" hidden="hidden"/>
                </div>

            </div>
        </div>
    </div>
</div>
<!--   //-----------弹出创建app的窗口------end----------      -->

<!--	//-----------弹出创建patch的窗口------start------------>
<div class="modal fade" id="myPatchModal" tabindex="-2" role="dialog" aria-labelledby="myPatchModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="height: 50px;">
                <div class="col-md-10">
                    <h4 class="modal-title" id="patchTitleUI">补丁</h4>
                </div>
                <div class="col-md-2">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                </div>
            </div>

            <div class="modal-body row">

                <div class="col-md-4 form-group">

                    <label>补丁编号</label>
                    <input type="text" class="form-control" id="createsn" name="createsn" required="required"/>

                </div>

                <div class="col-md-3 form-group">
                    <label>前置补丁编号</label>
                    <input type="text" class="form-control" id="createPatch_pre_sn" name="createPatch_pre_sn"/>
                </div>
                <div class="col-md-3 form-group">

                    <label>类别</label>
                    <select class="form-control" id="createPatch_patch_or_schema" name="createPatch_patch_or_schema">
                        <option selected="selected" value="0">patch</option>
                        <option value="1">schema</option>

                    </select>
                </div>
                <div class="col-md-2 form-group">
                    <label>启用</label>
                    <br>
                    <input type="checkbox" id="patchEnable" name="patchEnable" checked="checked"/>
                </div>
            </div>
            <div class="modal-body row ">

                <div class="col-md-12 form-group">
                    <label>描述</label>
                    <textarea class="wysihtml5 form-control" id="createPatch_desc" name="createPatch_desc"
                              rows="9"></textarea>
                </div>

            </div>
            <div class="modal-body row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label class="btn btn-danger btn-sm" for="patchFile">上传文件</label>
                        <img src="images/input-spinner.gif" id="showBusyImg" hidden="hidden"/>
                        <input type="file" id="patchFile" name="patchFile"
                               style="position:absolute;clip:rect(0 0 0 0);"/>
                        <label id="createurl_label"></label>
                    </div>
                </div>
            </div>
            <div class="modal-body row">
                <div class="col-md-6 form-group">
                    <label>md5</label>

                    <input type="text" readonly class="form-control" id="createmd5" name="createmd5"/>

                </div>
                <div class="col-md-2 form-group">

                </div>
                <div class="col-md-4  form-group">
                    <label>大小</label>
                    <input type="text" readonly class="form-control" id="createsize" name="createsize"/>

                </div>
            </div>

            <div class="modal-body row">

                <div class="col-md-5">

                </div>
                <div class="col-md-2">
                    <button class="btn btn-warning btn-sm" type="button" onClick="postPatchForm()">Save</button>

                </div>
                <div class="col-md-5">
                    <img src="images/input-spinner.gif" id="busyImg_savePatch" hidden="hidden"/>
                </div>
            </div>
        </div>
    </div>
</div>
<!--   //-----------弹出创建patch的窗口------end------------>

<!--	//-----------弹出推送 的窗口------start------------>
<div class="modal fade" id="pushPackageModal" tabindex="-3" role="dialog" aria-labelledby="pushPackageModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="height: 50px;">
                <div class="col-md-4">
                    <h4 class="modal-title">载入客户id列表</h4>
                </div>
                <div class="col-md-4">

                </div>
                <div class="col-md-4">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                </div>
            </div>
            <div class="modal-body row">
                <div class="col-md-3">
                    请选择用户列表
                </div>
                <div class="col-md-4">
                    <input type="file" id="useridFile" name="useridFile" onChange="onUseridFileChange()"></input>
                </div>
                <div class="col-md-1">
                    <img src="images/input-spinner.gif" id="showBusyImg2" hidden="hidden"/>
                </div>
                <div class="col-md-4">
                    <label id="uploadComment"></label>
                </div>
            </div>
            <div class="modal-body row">
                <div class="col-md-12">

                    <div class="form-group">
                        <table class="  table table-bordered">

                            <tbody id="sendUPVParamTable"></tbody>
                        </table>
                    </div>
                </div>
            </div>


            <div class="modal-body row">
                <div class="col-md-10">
                    <button type="button" class="btn btn-success btn-sm" id="uploadupvdataButton" disabled="disabled"
                            onclick="uploadUPVData()">确定</button>
                </div>
                <div class="col-md-2">

                </div>
            </div>
        </div>
    </div>
</div>
<!--   //-----------弹出推送 的窗口------end----------      -->

<!--	//-----------弹出app 描述的窗口------start------------>
<div class="modal fade" id="appDescModal" tabindex="-5" role="dialog" aria-labelledby="appDescModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="height: 50px;">
                <div class="col-md-4">
                    <h4 class="modal-title">app 描述</h4>
                </div>
                <div class="col-md-4">

                </div>
                <div class="col-md-4">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                </div>
            </div>


            <div class="modal-body row">

                <div class="col-md-12 ">
<span id="appDescLabel"> </span>
                </div>


            </div>
        </div>
    </div>
</div>
<!--   //-----------弹出app 描述的窗口------end------------>
<!-- Placed js at the end of the document so the pages load faster -->
<script src="/js/jquery-1.10.2.min.js"></script>
<script src="/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="/js/jquery-migrate-1.2.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/modernizr.min.js"></script>
<script src="/js/jquery.nicescroll.js"></script>
<script src="/js/jquery.ajaxfileupload.js"></script>
<script src="/js/ajaxfileupload.js"></script>
<!--dynamic table initialization -->
<!--common scripts for all pages-->
<script src="/js/scripts.js"></script>
<script src="/js/jquery.isotope.js"></script>
<script src="/js/html5shiv.js"></script>
<script src="/js/respond.min.js"></script>

<script type="text/javascript" src="/js/bootstrap-wysihtml5/wysihtml5-0.3.0.js"></script>
<script type="text/javascript" src="/js/bootstrap-wysihtml5/bootstrap-wysihtml5.js"></script>
<script>
    jQuery(document).ready(function () {
        $('.wysihtml5').wysihtml5({
            "font-styles": true, //Font styling, e.g. h1, h2, etc. Default true
            "emphasis": true, //Italics, bold, etc. Default true
            "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
            "html": false, //Button which allows you to edit the generated HTML. Default false
            "link": true, //Button to insert a link. Default true
            "image": false, //Button to insert an image. Default true,
            "color": false //Button to change color of font
        });
    });
    var totalCount = 0
    var currentPage = 1
    var step = 1000
    var currentTabIndex = 0
    var tablebody
    var tabContentList
    var currentAppListData
    var adddata
    var pageContent
    var host
    var allApp = 1
    var isNewApp = false

    //从API获取数据列表
    function getList(page, step) {
        $.get(host + "/list_app/" + page + "/step/" + step + "/all/" + allApp, null, listToSuccess, "json");
        currentPage = page;

    }
    //从数据库中获取列表成功
    function listToSuccess(response, status, xhr) {
        if (status == "success" && response.reusltCode == "200") {
            currentAppListData = response.reusltDetail;
            totalCount = response.totalCount;
            clearDataTable();
            if (currentAppListData.length > 0) {
                if (currentAppListData.length < (currentAppIndex + 1)) {
                    //非全部的时候可能只有一部分数据，所以可能会越界。
                    currentAppIndex = currentAppListData.length - 1;
                }
                currentAppId = currentAppListData[currentAppIndex].id;
            }

            fillTable();
        } else {
            alert(response);
        }
    }
    //页面载入后先获取默认条数的数据
    window.onload = function () {
        host = "http://" + window.location.host;
        getList(currentPage, step);
    };

    function onBodyResize() {
//document.getElementById("advtable").width = parent.document.body.clientWidth-210;
    }
    function filterData() {
        if (document.getElementById("includeall").checked == true) {
            allApp = 1;
        } else {
            allApp = 0;
        }
        getList(currentPage, step);
    }
    //采用本地数组中的数据填充表格
    function fillTable() {

        adddata = (' <li> <br/> &nbsp; <input onclick="filterData()" id="includeall" name="includeall" type="checkbox" ');
        if (allApp == 1) {
            adddata += (' checked ');
        }

        adddata += (' ><label style="color: #ffffff;">全部APP &nbsp;</label> </li>');

        pageContent = ""

        $.each(currentAppListData, function (index, item) {
            createAppContent(index, item);
        });
        adddata += (" <li  class='' > <a   data-toggle='modal' href='#myModal' onclick='isNewApp = true;clearAppForm();' class='btn btn-success  btn-sm' >新增APP</a></li>");
        tablebody.html(adddata);
        tabContentList.html(pageContent);
        getPatchList(1, currentPatchStep);
    }

    function createAppContent(index, item) {
        adddata += (" <li  ");
        if (index == currentAppIndex) {
            adddata += (" class='active' ");
            pageContent += (" <div style='padding: 0px;' class='tab-pane active' id='" + index + "'>");
        } else {
            adddata += (" class='' ");
            pageContent += (" <div  style='padding: 0px;'   class='tab-pane'    id='" + index + "'>");
        }
        adddata += (" > <a data-toggle='tab' id='tabItem" + index + "' name='tabItem" + index + "'   onclick='getPatchByAppid(" + item.id + "," + index + ")' href='#" + index + "'  >");
        adddata += ("<label title = " + item.bundle_id);

        var displayBundleid = item.bundle_id.split(".")
        var displayBundlelabel
        if (displayBundleid.length >= 2) {
            displayBundlelabel = displayBundleid[displayBundleid.length - 2] + "." + displayBundleid[displayBundleid.length - 1];
        } else {
            displayBundlelabel = item.bundle_id;
        }
        if (item.enable == 0) {
            adddata += (" style='color:#424242;'><s>" + displayBundlelabel + "</s></label></a>");
        } else {
            adddata += (">" + displayBundlelabel + "</label></a>");
        }


        adddata += ("</li>");
        pageContent += (' <table height="60px" width="100%">');
        pageContent += (' 	<tbody  bgcolor="#f9f9f9">');
        pageContent += (' 	<tr >');
        pageContent += (' 	<td>');
        pageContent += (' 			<label >App名称:</label>');
        pageContent += (' 			<label  id="bundle_id' + index + '" name="bundle_id' + index + '" >' + item.bundle_id + '</label>');
        if (item.enable == "0") {
            pageContent += (' 		(已禁用)');
        } else {
            pageContent += (' 		(已启用)');
        }
        pageContent += (' 	</td>');
        pageContent += (' 	<td>');
        pageContent += (' 			<label  >id:</label>');
        pageContent += (' 			<label id="appid' + index + '" name="appid' + index + '" >' + item.id + '</label>');
        pageContent += (' 	</td>');

        pageContent += (' 	<td>');
        pageContent += (' 			<label  >所属平台:</label>');
        pageContent += (' 			<label id="platform' + index + '" name="platform' + index + '" >' + getPlatformLabel(item.platform) + '</label>');

        pageContent += (' 	</td>');
        pageContent += (' 	<td>');
        pageContent += (' 			<label class="btn btn-success  btn-sm"   data-toggle="modal" href="#appDescModal"  onclick="setAppDescVisible()"  >查看描述</label>');

        pageContent += (' 	</td>');
        pageContent += (' 	<td>');
        pageContent += ('			<button    data-toggle="modal" href="#myModal" class="btn btn-success  btn-sm" onclick="isNewApp = false;editAppItem(' + index + ' ) " >Edit</button><a class="btn btn-success btn-sm"  data-toggle="modal"  href="#myPatchModal" onclick="clearPatchForm()" >添加补丁</a></td>');

        pageContent += (' 	</td>');
        pageContent += (' 	</tbody>');
        pageContent += (' 	</table><br/>');
        //----------------------------------------------
        pageContent += (' <table   width="100%">');
        pageContent += (' 	<thead>');
        pageContent += (' 	<tr >');

        pageContent += (' 			<th width="10%" >补丁编号</th>');
        //pageContent += (' 			<th width="20%">下载地址</th>');
        //			pageContent += (' 			<th width="15%">补丁描述</th>');
        pageContent += (' 			<th width="5%">size</th>');
        //				pageContent += (' 			<th width="20%">md5</th>');
        pageContent += (' 			<th width="10%">前置补丁</th>');
        pageContent += (' 			<th width="5%">类型</th>');
        pageContent += (' 			<th width="20%">修改时间</th>');
        pageContent += (' 			<th  align="right" width="10%"> </th>');
        pageContent += (' 		</tr>');
        pageContent += (' 	</thead>');
        pageContent += (' 	<tbody id="maintablebody' + index + '">');
        pageContent += (' 	</tbody>');
        pageContent += (' 	</table>');
        pageContent += ('</div>');
        return pageContent

    }
    function setAppDescVisible() {
        document.getElementById("appDescLabel").innerHTML = currentAppListData[currentAppIndex].description;
    }

    function getPlatformLabel(value) {

        switch (value) {
            case 0:
                return "iphone";
                break;
            case 1:
                return "android";
                break;
            case 2:
                return "ipad";
                break;
            case 3:
                return "android pad";
                break;
        }
        return "unknown"
    }

    function getPatchByAppid(appid, index) {
        currentAppId = appid;
        currentAppIndex = index;
        getPatchList(1, currentPatchStep);
    }


    function clearAppForm() {
        document.getElementById("createApp_bundle_id").value = "";
        $('#createApp_description').data("wysihtml5").editor.setValue("");
    }

    //点击编辑按钮的时候
    function addAppItem() {
        var currentdata = new Object();
        if (isNewApp == false) {
            currentdata.id = currentAppId;
        } else {
            currentdata.id = 0;
        }

        currentdata.bundle_id = document.getElementById("createApp_bundle_id").value;
        currentdata.platform = parseInt(document.getElementById("createApp_platform").value);
        currentdata.description = $('#createApp_description').val();
        if (currentdata.bundle_id == "") {
            document.getElementById("createApp_bundle_id").focus();
            alert("名称必填");
            return;
        }
        if (document.getElementById("appEnable").checked == true) {

            currentdata.enable = 1;
        } else {
            currentdata.enable = 0;
        }

        $.ajaxSetup({
            contentType: 'application/json'
        });
        document.getElementById("busyImg_saveAPP").hidden = "";
        $.post(host + "/add_app" + "/all/" + allApp, JSON.stringify(currentdata), postToSuccess);
    }
    //添加数据成功后，
    function postToSuccess(response, status, xhr) {
        if (status == "success" && (isNaN(response.id) == false)) {
            pushItem2list(currentAppListData, response)
            totalCount = currentAppListData.length
            //clearDataTable();
            if (currentAppListData.length > 0) {
                currentAppId = currentAppListData[currentAppIndex].id;
            }
            fillTable();
            $('#myModal').modal('hide');
        } else {
            alert(response);
        }
        document.getElementById("busyImg_saveAPP").hidden = "hidden";
    }

    function pushItem2list(oldList, item) {
        for (var i = 0; i < oldList.length; i++) {
            if (oldList[i].id == item.id) {
                oldList[i] = item;
                return;
            }
        }
        oldList = oldList.push(item);
    }
    //点击编辑按钮的时候
    function editAppItem(i) {
        currentTabIndex = i
        currentAppId = currentAppListData[i].id;
        document.getElementById("createApp_bundle_id").value = currentAppListData[i].bundle_id;

        document.getElementById("createApp_platform").value = getPlatformLabel(currentAppListData[i].platform);

        document.getElementById("createApp_platform").value = currentAppListData[i].platform;
        $('#createApp_description').data("wysihtml5").editor.setValue(currentAppListData[i].description);
        if (currentAppListData[i].enable == 1) {
            document.getElementById("appEnable").checked = true;
        } else {
            document.getElementById("appEnable").checked = false;
        }
        isNewApp = false;
    }
    //清空数据表格
    function clearDataTable() {
        tablebody = $("#appTabList");
        tablebody.empty();

        tabContentList = $("#appTableContentList");
        tabContentList.empty();
    }

    //------------------------------------------------------------------------------------------------------------------------
    //	var currentPatchId = 0
    var currentPatchPage = 1
    var currentAppId = 0
    var currentAppIndex = 0
    var currentAppName = ""
    var patchListdata
    var allAppPatch = 1
    var currentPatchStep = 10
    var patchTotalCount = 0

    //从API获取数据列表
    function getPatchList(tempPage, tempStep) {
        if (currentAppId > 0) {
            currentPatchPage = tempPage;
            currentPatchStep = tempStep;
            $.get(host + "/list_patch_by_app/" + currentAppId + "/page/" + currentPatchPage + "/step/" + currentPatchStep + "/all/" + allAppPatch, null, listPatchToSuccess, "json");
        }

    }
    //从数据库中获取列表成功
    function listPatchToSuccess(response, status, xhr) {

        if (status == "success" && response.reusltCode == "200") {
            patchListdata = response.reusltDetail;
            patchTotalCount = response.totalCount;

            changeStep(true);
            fillPatchTable();
            parent.setMainContentHeight(patchListdata.length, 50);
        } else {
            alert(response);
        }

    }
    function onBodyResize() {

    }
    //采用本地数组中的数据填充表格
    function fillPatchTable(templist) {
        var patchTablebody = $("#maintablebody" + currentAppIndex);
        var thisPatchlistData = "";
        $.each(patchListdata, function (index, item) {
            thisPatchlistData += ("<tr  style=' height:60px;'");
            //			if (item.enable == 0) {
            //				thisPatchlistData += ("  bgcolor='#FFFFdd' ");
            //			}
            var start_s = "<u><s>";
            var end_s = "</s></u>";
            if (item.enable == 1) {
                start_s = "";
                end_s = "";
            }
            thisPatchlistData += ("  name=" + index + " title = 'url : " + item.url + "'>");
            thisPatchlistData += ("<td>  " + start_s + item.sn + end_s + " </td>");
            //	thisPatchlistData += ("<td >" + start_s + item.md5 + end_s + " </td>");
            //				thisPatchlistData += ("<td>" + start_s + item.desc + end_s + "  </td>");
            thisPatchlistData += ("<td>" + start_s + item.size + end_s + "  </td>");
            //				thisPatchlistData += ("<td> " + start_s + item.desc + end_s + "  </td>");
            thisPatchlistData += ("<td> " + start_s + item.pre_sn + end_s + "   </td>");
            thisPatchlistData += ("<td> " + start_s + getPatchType(item.patch_or_schema) + end_s + " </td>");
            thisPatchlistData += ("<td> " + start_s + item.createtime + end_s + " </td>");
            thisPatchlistData += ("<td  align='right'><a  class='btn btn-success  btn-sm'  data-toggle='modal' href='#myPatchModal' onclick='fillPatchForm(" + item.id + "," + index + ")' >编辑</a>");
            if (currentAppListData[currentAppIndex].enable == 0 || item.enable == 0) {

                thisPatchlistData += (" <button  class='btn btn-danger  btn-sm'  disabled='disabled'   >推送</button></td>"); //
            } else {
                thisPatchlistData += (" <button  class='btn btn-danger  btn-sm'  data-toggle='modal' href='#pushPackageModal' onclick='selectPushForm(" + index + ")'  >推送</button></td>"); //
            }
            thisPatchlistData += "</tr>";
        });
        patchTablebody.html(thisPatchlistData);
    }
    //每页的条数发生改变时  重新计算分页条数
    function changeStep(onlyChangeStep) {
        var pagelabelDiv = $("#pageLabelDiv ul");
        pagelabelDiv.empty();
        var stepinput = document.getElementById("stepInput")
        currentPatchStep = parseInt(stepinput.options[stepinput.selectedIndex].value);
        var pageNum = Math.ceil(patchTotalCount / currentPatchStep);
        if (currentPatchPage <= 1) {
            pagelabelDiv.append('<li class="prev disabled"><a href="#">← Previous</a></li>')
        } else {
            pagelabelDiv.append('<li class="prev" onclick="getPatchList(' + (currentPatchPage - 1) + ',' + currentPatchStep + ')" ><a href="#">← Previous</a></li>')
        }
        if (pageNum > 6) {
            for (var i = 1; i <= pageNum; i++) {
                if ((i) == currentPatchPage) {
                    pagelabelDiv.append('<li class="active"><a   >' + (i) + '</a></li>')
                } else if ((i <= 2)) {
                    pagelabelDiv.append('<li onclick="getPatchList(' + i + ',' + currentPatchStep + ')"><a  href="#">' + (i) + '</a></li>')
                } else if ((i >= currentPatchPage - 1 && (i <= currentPatchPage + 1)) || (i > pageNum - 2)) {
                    pagelabelDiv.append('<li onclick="getPatchList(' + i + ',' + currentPatchStep + ')"><a  href="#">' + (i) + '</a></li>')
                }
            }

        } else {
            for (var i = 1; i <= pageNum; i++) {
                if ((i) == currentPatchPage) {
                    pagelabelDiv.append('<li class="active"><a   >' + (i) + '</a></li>')
                } else {
                    pagelabelDiv.append('<li onclick="getPatchList(' + i + ',' + currentPatchStep + ')"><a  href="#">' + (i) + '</a></li>')
                }
            }
        }
        if (currentPatchPage >= pageNum) {
            pagelabelDiv.append('<li class="next disabled"><a href="#">Next → </a></li>')
        } else {
            pagelabelDiv.append('<li class="next" onclick="getPatchList(' + (currentPatchPage + 1) + ',' + currentPatchStep + ')" ><a href="#">Next → </a></li>')
        }
        if (onlyChangeStep == false) {
            getPatchList(1, currentPatchStep);
        }

    }
    //点击新增patch的按钮
    function clearPatchForm() {
        //	currentPatchId = 0;
        currentpatch = null;
        document.getElementById("createsn").value = "";
        //document.getElementById("createPatch_url").value = "";
        document.getElementById("createmd5").value = "";
        document.getElementById("createsize").value = "";
        $('#createPatch_desc').data("wysihtml5").editor.setValue("");
        document.getElementById("createPatch_pre_sn").value = "";
        document.getElementById("createurl_label").textContent = "";
        //document.getElementById("enable").checked = true;

    }
    var currentpatch
    //点击patch修改按钮
    function fillPatchForm(patchid, index) {
        currentpatch = patchListdata[index]
        //	currentPatchId = patchid;

        document.getElementById("patchTitleUI").textContent = currentAppListData[currentAppIndex].bundle_id + "补丁";
        document.getElementById("createsn").value = currentpatch.sn;
        document.getElementById("createurl_label").textContent = currentpatch.url;
        document.getElementById("createmd5").value = currentpatch.md5;
        document.getElementById("createsize").value = currentpatch.size;
        $('#createPatch_desc').data("wysihtml5").editor.setValue(currentpatch.desc);

        //	document.getElementById("createPatch_desc").value = currentpatch.desc;
        document.getElementById("createPatch_pre_sn").value = currentpatch.pre_sn;
        if (currentpatch.patch_or_schema != "") {
            document.getElementById("createPatch_patch_or_schema").value = currentpatch.patch_or_schema;
        }
        if (currentpatch.enable == 1) {
            document.getElementById("patchEnable").checked = true;
        } else {
            document.getElementById("patchEnable").checked = false;
        }
    }

    function getPatchType(value) {
        switch (value) {
            case 0:
                return "patch";
                break;
            case 1:
                return "schema";
                break;
        }
    }

    function selectPushForm(index) {

        currentpatch = patchListdata[index];


        var adddata = "";


        adddata += ("<tr > ");
        adddata += ("<td align='right'>平台:</td>");
        adddata += ("<td ><b>" + getPlatformLabel(currentAppListData[currentAppIndex].platform) + "</b></td>");
        adddata += ("<td align='right'>size:</td>");
        adddata += ("<td ><b>" + currentpatch.size + "</b></td>");
        adddata += "</tr>";


        adddata += ("<tr > ");
        adddata += ("<td align='right'>bundle id:</td>");
        adddata += ("<td ><b>" + currentAppListData[currentAppIndex].bundle_id + "</b></td>");

        adddata += ("<td align='right'>补丁编号:</td>");
        adddata += ("<td ><b>" + currentpatch.sn + "</b></td>");
        adddata += "</tr>";

        adddata += ("<tr > ");
        adddata += ("<td align='right' >md5:</td>");
        adddata += ("<td ><b>" + currentpatch.md5 + "</b></td>");
        adddata += ("<td align='right'>前置补丁:</td>");
        adddata += ("<td ><b>" + currentpatch.pre_sn + "</b></td>");

        adddata += "</tr>";

        adddata += ("<tr >");
        adddata += ("<td align='right'>描述:</td>");
        adddata += ("<td colspan='3'><b>" + currentpatch.desc + "</b></td>");
        adddata += "</tr>";

        adddata += ("<tr > ");
        adddata += ("<td align='right'>地址:</td>");
        adddata += ("<td colspan='3'><b>" + currentpatch.url + "</b></td>");

        adddata += "</tr>";

        $("#sendUPVParamTable").empty();
        $("#sendUPVParamTable").append(adddata);
        // 把用户文件这个空间清空。免得误操作
        document.getElementById("useridFile").value = null;
    }

    //点击提交按钮
    function postPatchForm() {
        var currentdata = new Object();
        if (currentpatch != null) {
            currentdata.id = currentpatch.id;
        }
        currentdata.t_app_id = currentAppId;
        currentdata.bundle_id = currentAppListData[currentAppIndex].bundle_id;
        currentdata.sn = document.getElementById("createsn").value;
        currentdata.url = document.getElementById("createurl_label").textContent;
        currentdata.md5 = document.getElementById("createmd5").value;
        currentdata.size = parseInt(document.getElementById("createsize").value, 10);
        currentdata.desc = $('#createPatch_desc').val(); //document.getElementById("createPatch_desc").value;

        currentdata.pre_sn = document.getElementById("createPatch_pre_sn").value;
        if (currentdata.pre_sn == "") {
            currentdata.pre_sn = "null";
        }
        currentdata.patch_or_schema = parseInt(document.getElementById("createPatch_patch_or_schema").value);
        if (document.getElementById("patchEnable").checked == true) {
            currentdata.enable = 1;
        } else {
            currentdata.enable = 0;
        }
        currentdata.page = currentPatchPage;
        currentdata.step = currentPatchStep;
        if (currentdata.sn == "") {
            document.getElementById("createsn").focus();
            alert("补丁编号必填");
            return;
        }
        document.getElementById("busyImg_savePatch").hidden = "";

        $.post(host + "/add_patch", JSON.stringify(currentdata), addPatchToSuccess);
    }
    function addPatchToSuccess(response, status, xhr) {
        if (response.reusltCode == "200") {
            patchListdata = response.reusltDetail;
            fillPatchTable();
            $('#myPatchModal').modal('hide');
        } else {
            alert(response)
        }
        document.getElementById("busyImg_savePatch").hidden = "hidden";

    }

    //点击删除按钮，先确认
    function confirmPatchDelete(id, enable) {
        if (confirm('确定要删除？')) {
            $.get(host + "/delete_patch/" + id + "/enable/" + enable, null, listPatchToSuccess);
        }
    }

    $("#patchFile").ajaxfileupload({
        action: '/uploadPatchfile',
        params: {
            appid: function () {
                return currentAppId;
            }
        },
        valid_extensions: ['gif', 'png', 'jpg', 'jpeg', 'rar', 'zip', 'patch', 'apk', 'gz'],
        type: 'post',
        onStart: function () {
            var extList = document.getElementById("patchFile").value.split(".");
            var ext = extList[extList.length - 1];
            if (!(/^(rar|zip|patch|tar|apk|png|gz|jpeg|jpg)$/.test(ext))) {
                alert("您上传的文档格式不对，请重新选择！");
                return false;
            }

            document.getElementById("showBusyImg").hidden = "";
        },
        onComplete: function (response) {
            document.getElementById("showBusyImg").hidden = "hidden";
            if (response.url == undefined) {
                if (response.status == false) {
                    alert(response.message);
                } else {

                    alert(response);
                }
                return;
            }
            document.getElementById("patchFile").value = null;
            document.getElementById("createsize").value = response.size;
            document.getElementById("createmd5").value = response.md5;
            document.getElementById("createurl_label").innerText = response.url;
        }
    });

    function onUseridFileChange() {

        if (document.getElementById("useridFile").value) {
            var extList = document.getElementById("useridFile").value.split(".");
            var ext = extList[extList.length - 1];
            if (!(/^(txt|ini)$/.test(ext))) {
                alert("uid串必须是用英文逗号间隔的txt文档");
                return false;
            }
        }
        document.getElementById('uploadupvdataButton').disabled = "";
    }

    function uploadUPVData() {
        // 开始上传文件时显示一个图片
        $("#showBusyImg2").ajaxStart(function () {
            $(this).show();
            // 文件上传完成将图片隐藏起来
        }).ajaxComplete(function () {
            $(this).hide();
        });
        //	var elementIds = ["flag"]; //flag为id、name属性名
        var data = {
            bundleid: currentAppListData[currentAppIndex].bundle_id,
            //	typeValue: currentAppListData[currentAppIndex].type,
            platform: currentAppListData[currentAppIndex].platform,
            //appid:currentAppListData[currentAppIndex].id,
            tPatchID: currentpatch.id,
            md5: currentpatch.md5,
            url: currentpatch.url,
            desc: currentpatch.desc,
            size: currentpatch.size,
            prevPatchID: currentpatch.pre_sn
        }
        data["typeValue"] = currentpatch.patch_or_schema;

        $.ajaxFileUpload({
            url: '/upload_upv_file',
            type: 'post',
            secureuri: false, //一般设置为false
            fileElementId: 'useridFile', // 上传文件的id、name属性名
            data: data,
            dataType: 'json', //返回值类型，一般设置为json、application/json
            //	elementIds: elementIds, //传递参数到服务器
            success: function (response, status) {
                if (status == "success") {
                    $('#pushPackageModal').modal('hide');
                }
                var result = alert(response);
                if (result) {
                    //	parent.getElementById("maincontent").removeAll()
                    //    parent.location.reload();
                } else {
                    //   parent.location.reload();
                }

            },
            error: function (data, status, e)//服务器响应失败处理函数
            {
                alert(e);
                // parent.location.reload();
            }
        });

        //return false;
    }

</script>
</body>